# _*_coding:utf-8 _*_
# @Author : 袁小双
# @Email  : yuanxs29422@hundsun.com
# @Time   : 2021/8/9 19:30
# @File   : compareData.py

import re
from datetime import datetime
from BladeFerm.Lib.Common import logger

def cmp_date(target_time, time_start='1970-07-01', time_end='2120-06-01', format_pattern='%Y-%m-%d'):
    '''
    校验目标日期是否在校验范围之内
    :param target_time: 
    :param time_start: 
    :param time_end: 
    :param format_pattern: 
    :return: 
    '''
    flag = False
    if not time_start and time_start == ' ':
        time_start = '1970-07-01'
    if not time_end and time_end == ' ':
        time_end = '2220-01-01'
    cmp_start = (datetime.strptime(target_time, format_pattern) - datetime.strptime(time_start, format_pattern))
    cmp_end = (datetime.strptime(time_end, format_pattern) - datetime.strptime(target_time, format_pattern))
    if cmp_start.days < 0 or cmp_end.days < 0:
        flag = False
    else:
        flag = True
    logger.info(f'目标日期:{target_time}，日期校验范围：[{time_start},{time_end}]，校验结果是：{flag}')
    return True

def cmp_value(value, min, max):
    '''
    校验目标值是否在最大和最小值之间
    :param value: 目标值
    :param min:
    :param max:
    :return:
    '''
    res = True if float(min) <= float(value) <= float(max) else False
    logger.info(f'目标值:{value}，期望范围：[{min},{max}]，校验结果是：{res}')
    return res


def cmp_contain(targetList, expectList):
    '''
    利用期望列表中的检索词，检索列表中每个元素是否包含检索词
    :param targetList: 目标字符串
    :param expectList: 检索词列表
    :return:
    '''
    flag = True
    expectList = [expectList] if not isinstance(expectList, list) else expectList
    for exp in expectList:
        res = re.search(exp, targetList, re.I)
        if not res:
            flag = False
            logger.info(f'检索信息:{targetList}，检索词：{exp}，校验结果是：{flag}')
        else:
            logger.info(f'检索信息:{targetList}，检索词：{exp}，校验结果是：{flag}')
    return flag

def cmp_count(reslst,index):
    if len(reslst) > index:
        check_res = True
    else:
        check_res = False
    return check_res

def cmp_state(min, max):
    '''
    判断：只有2个条件都真时为真，其他都为假
    :param min: 
    :param max: 
    :return: 
    '''
    s_1 = 1 if not min or min == ' ' else 0
    s_2 = 1 if not max or max == ' ' else 0
    return True if s_1&s_2 == 1 else False
